package main

import (
	"context"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"inventory_srv/inventory_proto"
	"log"
	"sync"
)

func main() {
	conn, err := grpc.Dial("10.3.189.13:50051", grpc.WithBlock(), grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Println(err)
		return
	}
	client := inventory_proto.NewInventoryClient(conn)

	gInfo := []*inventory_proto.GoodsInvInfo{
		{
			GoodsID: 1,
			Nums:    1,
		},
	}

	wg := sync.WaitGroup{}
	wg.Add(100)

	for i := 0; i < 100; i++ {
		go func() {
			defer wg.Done()
			_, err = client.Sell(context.Background(), &inventory_proto.SellInfo{
				GoodsInfo: gInfo,
			})
			if err != nil {
				log.Println(err)
				return
			}
			log.Println("扣减成功")
		}()
	}

	wg.Wait()
}
